package com.bullsora.kindlerss.repository;

import com.bullsora.kindlerss.model.FeedEntry;
import com.bullsora.kindlerss.model.FeedUrl;
import com.bullsora.kindlerss.model.Subscription;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.Query;

import java.util.List;

/**
 * @author sorin.buliarca@iquesint.com
 */
public class FeedRepository {

  private GenericOperations genericOperations;

  public GenericOperations getGenericOperations() {
    return genericOperations;
  }

  public void setGenericOperations(GenericOperations genericOperations) {
    this.genericOperations = genericOperations;
  }

  public FeedUrl getFeedUrlByUrl(String url) {
    return getObjectify().query(FeedUrl.class).filter(FeedUrl.URL, url).get();
  }

  private Objectify getObjectify() {
    return genericOperations.getObjectify();
  }

  public boolean isExistentEntry(String link, String feedUrl) {
    return getObjectify().query(FeedEntry.class).filter(FeedEntry.LINK, link).filter(FeedEntry.FEED_URL, feedUrl).count() != 0;
  }

  public List<FeedEntry> getNewEntries(Subscription subscription, String url) {
    Query<FeedEntry> query = getObjectify().query(FeedEntry.class);
    return query.filter("receiveDate > ", subscription.getLastChecked()).filter("feedUrl", url).list();
  }
}
